<template>
  <div style="width: 500px;">
      <el-input style="margin-bottom: 5px;"
      placeholder="请输入部门名称进行搜索..." 
      prefix-icon="el-icon-search" 
      v-model="filterText">
      </el-input>

      <el-tree class="filter-tree" :data="deps" 
        :props="defaultProps" 
        :expand-on-click-node="false"
        :filter-node-method="filterNode" ref="tree">
        <span class="custom-tree-node" slot-scope="{ node, data }" style="display: flex;justify-content: space-between;width: 100%;">
          <span>{{ data.name }}</span>
          <span>
            <el-link type="primary" @click="() => showAddDep(data)" style="margin-right: 7px;">添加</el-link>
            <el-link type="danger" @click="() => deleteDep(data)">删除</el-link>
          </span>
        </span>
      </el-tree>

      <el-dialog title="添加部门" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
       <div>
           <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>
              {{pname}}
            </td>
           </tr>
           <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
            </td>
           </tr>
       </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doAddDep">确 定</el-button>
        </span>
      </el-dialog>
  </div>

</template>

<script>
export default {
  data() {
    return {
      // 搜索部门
      filterText: '',
      // 部门数据
      deps: [],
      // tree 绑定
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 添加弹框触发
      dialogVisible: false,
      // 添加数据
      dep: {
        name: '',
        parentId : ''
      },
      // 上级名
      pname : ''
    }
  },
  methods: {
    // 移除部门
    addDep2Dep2(deps,dep) {
      for (let i = 0; i < deps.length; i++){
        let d = deps[i];
        if (d.id == dep.parentId) {
          d.children = d.children.concat(dep);
          return;
        } else {
          // 递归
          this.addDep2Dep2(d.children, dep);
        }
      }
    },
    // 删除清除数据
    initDep() {
      this.dep = {
        name: '',
        parentId : -1 
      }
      this.pname = ''; 
    },
    // 添加部门
    doAddDep() {
      this.postRequest('/system/basic/department/',this.dep).then(resp => {
        if (resp) {
          // 添加成功后返回 
          this.addDep2Dep2(this.deps,resp.obj)
          //关闭弹框 
          this.dialogVisible = false;
          // 重新初始化
          this.initDep();
        }
      })
    },
    // 显示添加部门
    showAddDep(data) {
      this.dep.parentId = data.id;   
      this.pname = data.name;
      this.dialogVisible = true;
      console.log(data)
    },
    // 删除部门
    deleteDep(data) {

      if (data.isParent) {
        this.$message.error('父部门删除失败')
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 删除部门
          this.deleteRequest('/system/basic/department/' + data.id).then(resp => {
            if (resp) {
                this.removeDepFromDeps(this.deps, data.id);
              }
          })

          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
    // 删除
    removeDepFromDeps(deps,id) {
      for (let i = 0; i < deps.length; i++) {
        let d = deps[i];
        if (d.id == id) {
          // 重现在当前索引删除一个
          deps.splice(i, 1);
          return;          
        } else {
          this.removeDepFromDeps(d.children, id);
        }
      }
    },
    // 初始化部门
    initDeps() {
      
      this.getRequest('/system/basic/department/').then(resp => {
        if (resp) {
           this.deps = resp;
          }
       })
    },
    // 拦截节点
    filterNode(value, data) {
      // true 可以进行展示
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    }
  },
  mounted() {
    this.initDeps();
  },
  watch: {
    // 观察事件
    filterText (val) {
      this.$refs.tree.filter(val);
    },
    
  }
}
</script>

<style>

</style>